//
//  LRXJudgeMentController.m
//  路人侠
//
//  Created by zhangxulong on 4/9/16.
//  Copyright © 2016 zhangxulong. All rights reserved.
//

#import "LRXJudgeMentController.h"

@interface LRXJudgeMentController ()<UITextViewDelegate>
@property (nonatomic,assign) CGFloat nowY;
@property (nonatomic,weak) UITextView * mesgView;
@property (nonatomic,assign) CGFloat inputViewY;
@end

@implementation LRXJudgeMentController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.view.backgroundColor = [UIColor whiteColor];
    [self layoutNaveView];
    
    [self layoutHeadView];
    [self layoutContentView];
    [self wMessage];
}
// 设置 导航栏 的设置
- (void)layoutNaveView
{
    //1.1 设置标题
    self.navigationItem.title = @"发表评价";
    UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    //颜色
    [rightBtn setTitleColor:[UIColor colorWithRed:253.0  green:253.0  blue:253.0 ] forState:UIControlStateNormal];
    //图片和文字的布
    [rightBtn setImage:[UIImage imageNamed:@"checkIcon"] forState:UIControlStateNormal];
    //大小
    rightBtn.bounds = CGRectMake(0, 0, 30 * wScale, 36 * hScale);
    UIBarButtonItem * rightBtnItem = [[UIBarButtonItem alloc]initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = rightBtnItem;
    [rightBtn addTarget:self action:@selector(checkOrder) forControlEvents:UIControlEventTouchUpInside];
}

- (void)layoutHeadView
{
    UIImageView * userImage = [[UIImageView alloc]init];
    userImage.frame = CGRectMake(90 * wScale, 34 * hScale, 81 * hScale, 81 * hScale);
    userImage.image = [UIImage imageNamed:@"de"];
    userImage.layer.cornerRadius = userImage.frame.size.width * 0.5;
    userImage.layer.masksToBounds = YES;
    [self.view addSubview:userImage];
    
    
    UILabel * userName = [[UILabel alloc]init];
    userName.text = @"XXXXX";
    userName.textColor = [UIColor colorWithRed:115 green:115 blue:115];
    userName.font = [UIFont systemFontOfSize:24 * hScale];
    [userName sizeToFit];
    userName.center = CGPointMake(userImage.center.x, CGRectGetMaxY(userImage.frame) + 31 * hScale);
    [self.view addSubview:userName];
    
    
    UILabel * serReLabel = [[UILabel alloc]init];
    serReLabel.text = @"极好极好极好极好极好极";
    serReLabel.font = [UIFont systemFontOfSize:24 * hScale];
    serReLabel.frame = CGRectMake(300 * wScale, 56 * hScale, 300 * wScale, 24 * hScale);
    
    UILabel * speeReLabel = [[UILabel alloc]init];
    speeReLabel.text = @"极好极好极好";
    speeReLabel.font = [UIFont systemFontOfSize:24 * hScale];
    speeReLabel.frame = CGRectMake(300 * wScale, 108 * hScale, 300 * wScale, 24 * hScale);

    CGFloat labelX = CGRectGetMinX(speeReLabel.frame) - 57 * wScale;
    
    UILabel * serLabel = [[UILabel alloc]init];
    serLabel.text = @"服务:";
    serLabel.textColor = [UIColor colorWithRed:115 green:115 blue:115];
    serLabel.font = [UIFont systemFontOfSize:24 * hScale];
    [serLabel sizeToFit];
    serLabel.center = CGPointMake(labelX, serReLabel.center.y);
  
    
    UILabel * speeLabel = [[UILabel alloc]init];
    speeLabel.text = @"速度:";
    speeLabel.textColor = [UIColor colorWithRed:115 green:115 blue:115];
    speeLabel.font = [UIFont systemFontOfSize:24 * hScale];
    [speeLabel sizeToFit];
    speeLabel.center = CGPointMake(labelX, speeReLabel.center.y);
    
    
    
    [self.view addSubview:serReLabel];
    [self.view addSubview:speeReLabel];
    [self.view addSubview:serLabel];
    [self.view addSubview:speeLabel];
    
    UIView * lineView = [[UIView alloc]init];
    lineView.backgroundColor = [UIColor colorWithRed:238 green:238 blue:238];
    lineView.frame = CGRectMake(0, CGRectGetMaxY(userName.frame) + 34 * hScale, MainScreenW, 2 * hScale);
    [self.view addSubview:lineView];
    
    self.nowY = CGRectGetMaxY(lineView.frame);
}
- (void)layoutContentView
{
    CGFloat startBtnCenterY  = 0;
    for (int i = 0; i < 5; i++) {
        UIButton * startBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        startBtn.tag = i + 1;
        CGFloat startBtnX = 127 * wScale + (i * 90 * wScale);
        startBtn.frame = CGRectMake(startBtnX, self.nowY + 38 * hScale, 53 * hScale, 51 * hScale);
        [startBtn setImage:[UIImage imageNamed:@"pingjia"] forState:UIControlStateNormal];
        [startBtn setImage:[UIImage imageNamed:@"pingjiaed"] forState:UIControlStateSelected];
        [self.view addSubview:startBtn];
        startBtnCenterY = startBtn.center.y;
        
    }
    CGFloat startBtn2CenterY  = 0;
    for (int i = 0; i < 5; i++) {
        UIButton * startBtn2 = [UIButton buttonWithType:UIButtonTypeCustom];
        startBtn2.tag = i + 6;
        CGFloat startBtn2X = 127 * wScale + (i * 90 * wScale);
        startBtn2.frame = CGRectMake(startBtn2X, self.nowY + 128 * hScale, 53 * hScale, 51 * hScale);
        [startBtn2 setImage:[UIImage imageNamed:@"pingjia"] forState:UIControlStateNormal];
        [startBtn2 setImage:[UIImage imageNamed:@"pingjiaed"] forState:UIControlStateSelected];
        [self.view addSubview:startBtn2];
         startBtn2CenterY = startBtn2.center.y;
        
    }
    
    CGFloat labelX = 55 * wScale;
    
    UILabel * serLabel = [[UILabel alloc]init];
    serLabel.text = @"服务:";
    serLabel.font = [UIFont systemFontOfSize:28 * hScale];
    [serLabel sizeToFit];
    serLabel.center = CGPointMake(labelX, startBtnCenterY);
    
    
    UILabel * speeLabel = [[UILabel alloc]init];
    speeLabel.text = @"速度:";
    speeLabel.font = [UIFont systemFontOfSize:28 * hScale];
    [speeLabel sizeToFit];
    speeLabel.center = CGPointMake(labelX, startBtn2CenterY);
    
    UIView * lineView = [[UIView alloc]init];
    lineView.backgroundColor = [UIColor colorWithRed:238 green:238 blue:238];
    lineView.frame = CGRectMake(0, CGRectGetMaxY(speeLabel.frame) + 49 * hScale, MainScreenW, 2 * hScale);
    
    [self.view addSubview:lineView];
    [self.view addSubview:serLabel];
    [self.view addSubview:speeLabel];
    self.nowY = CGRectGetMaxY(lineView.frame);
    
}

- (void)checkOrder
{
    
}
- (void)pushViewController
{
    [self.navigationController popViewControllerAnimated:YES];
}
//留言 板块
- (void)wMessage
{
    
    UITextView * megText = [[UITextView alloc]init];
    megText.frame = CGRectMake(25 * wScale, self.nowY + 30 * hScale, MainScreenW - 56 * wScale, self.view.frame.size.height - self.nowY - 30 * hScale);
    megText.font = [UIFont systemFontOfSize:24 * hScale];
    megText.text = @"综合评价?";
    megText.delegate = self;
    
    self.mesgView = megText;

    megText.textColor = [UIColor colorWithRed:115 green:115 blue:115];
    [self.view addSubview:megText];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
    

}
//事实证明 前调用 这个函数
- (void)keyboardWillChangeFrame:(NSNotification *)noti
{
    CGRect frame = [noti.userInfo[@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];
    self.inputViewY = frame.origin.y;
}
//调用完keyboardWillChangeFrame 这个函数后，在调用这个函数
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    NSTimeInterval animationDuration = 0.30f;
    
    [UIView beginAnimations:@"ResizeForKeyBoard"context:nil];
    
    [UIView setAnimationDuration:animationDuration];
    
    float width = self.view.frame.size.width;
    
    float height = self.view.frame.size.height;
    
    CGFloat sizeY = [textView.text sizeWithMaxSize:textView.frame.size fontSize:24 * hScale].height + textView.frame.origin.y;
    CGFloat offset = sizeY - self.inputViewY + 50 * hScale;
    if (offset < 0) {
        offset = 0;
    }
    
    CGRect rect = CGRectMake(0.0f, offset , width, height);
    
    self.view.frame = rect;
    
    [UIView  commitAnimations];
}
////编辑的 时候
- (void)textViewDidChange:(UITextView *)textView{
    float offset = 0.0f;
    
    
    if(self.mesgView == textView)
    {
        CGFloat sizeY = [textView.text sizeWithMaxSize:textView.frame.size fontSize:24 * hScale].height + textView.frame.origin.y + 50 * hScale;
        if (sizeY < self.inputViewY) {
            offset = sizeY - self.inputViewY;
        }
        
    }
    
    NSTimeInterval animationDuration = 0.30f;
    
    [UIView beginAnimations:@"ResizeForKeyBoard"context:nil];
    
    [UIView setAnimationDuration:animationDuration];
    
    float width = self.view.frame.size.width;
    
    float height = self.view.frame.size.height;
    
    CGRect rect = CGRectMake(0.0f, -offset , width, height);
    
    self.view.frame = rect;
    
    [UIView  commitAnimations];
    
    
}


@end
